package com.example.petshopping.ui.orders;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.petshopping.R;
import com.example.petshopping.ui.orders.model.Order;

public class OrderDetailActivity extends AppCompatActivity {

    private static final String TAG = "OrderDetailActivity";
    
    // UI elements
    private TextView tvStoreName;
    private ImageView ivStoreAvatar;
    private ImageView ivProductImage;
    private TextView tvProductTitle;
    private TextView tvProductPrice;
    private TextView tvProductQuantity;
    private TextView tvOrderId;
    private TextView tvOrderTime;
    private TextView tvPaymentMethod;
    private TextView tvSalePrice;
    private TextView tvDiscount;
    private TextView tvOtherDiscount;
    private TextView tvShippingFee;
    private TextView tvOtherFee;
    private TextView tvTotalAmount;
    private TextView tvAddress;
    private TextView tvContact;
    private Button btnProductDetail;
    private Button btnBuyAgain;
    
    // Data
    private String orderId;
    private Order order;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            setContentView(R.layout.activity_order_detail);
            
            // Get order ID from intent
            orderId = getIntent().getStringExtra("order_id");
            if (orderId == null || orderId.isEmpty()) {
                Log.e(TAG, "No order ID provided");
                Toast.makeText(this, "订单信息不存在", Toast.LENGTH_SHORT).show();
                finish();
                return;
            }
            
            initViews();
            loadOrderData();
            setupListeners();
            
        } catch (Exception e) {
            Log.e(TAG, "Error in onCreate: ", e);
            Toast.makeText(this, "页面加载失败: " + e.getMessage(), Toast.LENGTH_SHORT).show();
            finish();
        }
    }
    
    private void initViews() {
        try {
            // Store info
            tvStoreName = findViewById(R.id.tv_store_name);
            ivStoreAvatar = findViewById(R.id.iv_store_avatar);
            
            // Product info
            ivProductImage = findViewById(R.id.iv_product_image);
            tvProductTitle = findViewById(R.id.tv_product_title);
            tvProductPrice = findViewById(R.id.tv_product_price);
            tvProductQuantity = findViewById(R.id.tv_product_quantity);
            
            // Order info
            tvOrderId = findViewById(R.id.tv_order_id);
            tvOrderTime = findViewById(R.id.tv_order_time);
            tvPaymentMethod = findViewById(R.id.tv_payment_method);
            tvSalePrice = findViewById(R.id.tv_sale_price);
            tvDiscount = findViewById(R.id.tv_discount);
            tvOtherDiscount = findViewById(R.id.tv_other_discount);
            tvShippingFee = findViewById(R.id.tv_shipping_fee);
            tvOtherFee = findViewById(R.id.tv_other_fee);
            tvTotalAmount = findViewById(R.id.tv_total_amount);
            
            // Address info
            tvAddress = findViewById(R.id.tv_address);
            tvContact = findViewById(R.id.tv_contact);
            
            // Buttons
            btnProductDetail = findViewById(R.id.btn_product_detail);
            btnBuyAgain = findViewById(R.id.btn_buy_again);
            
            // Back button
            findViewById(R.id.btn_back).setOnClickListener(v -> finish());
        } catch (Exception e) {
            Log.e(TAG, "Error initializing views: ", e);
            throw e;
        }
    }
    
    private void loadOrderData() {
        try {
            // In a real app, this would fetch data from a database or API
            // For demo purposes, we'll use mock data
            
            // Mock order data
            order = createMockOrder(orderId);
            
            // Populate UI with order data
            displayOrderData();
        } catch (Exception e) {
            Log.e(TAG, "Error loading order data: ", e);
            Toast.makeText(this, "无法加载订单数据: " + e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }
    
    private Order createMockOrder(String orderId) {
        Order order = new Order();
        order.setId(orderId);
        order.setStoreName("店铺名称");
        order.setStoreAvatarResId(R.drawable.yingwu);
        order.setStatus(Order.STATUS_COMPLETED);
        order.setProductTitle("产品标题产品标题产品标题产品标题产品标题产品标题");
        order.setProductImageResId(R.drawable.yugang);
        order.setPrice("¥199.99");
        order.setQuantity(1);
        order.setTotalAmount("¥199.99");
        
        // Additional order detail fields
        order.setOrderTime("2025-11-17 10:12:09");
        order.setPaymentMethod("微信支付");
        order.setSalePrice("¥199.99");
        order.setDiscount("¥0.00");
        order.setOtherDiscount("¥0.00");
        order.setShippingFee("¥0.00");
        order.setOtherFee("¥0.00");
        order.setAddress("山东省济南市历下区浪潮科技园S06栋");
        order.setContact("历雪梅 15210010866");
        
        return order;
    }
    
    private void displayOrderData() {
        // Store info
        tvStoreName.setText(order.getStoreName());
        ivStoreAvatar.setImageResource(order.getStoreAvatarResId());
        
        // Product info
        ivProductImage.setImageResource(order.getProductImageResId());
        tvProductTitle.setText(order.getProductTitle());
        tvProductPrice.setText(order.getPrice());
        tvProductQuantity.setText("x" + order.getQuantity());
        
        // Order info
        tvOrderId.setText(order.getId());
        tvOrderTime.setText(order.getOrderTime());
        tvPaymentMethod.setText(order.getPaymentMethod());
        tvSalePrice.setText(order.getSalePrice());
        tvDiscount.setText(order.getDiscount());
        tvOtherDiscount.setText(order.getOtherDiscount());
        tvShippingFee.setText(order.getShippingFee());
        tvOtherFee.setText(order.getOtherFee());
        tvTotalAmount.setText(order.getTotalAmount());
        
        // Address info
        tvAddress.setText(order.getAddress());
        tvContact.setText(order.getContact());
    }
    
    private void setupListeners() {
        // Product detail button
        btnProductDetail.setOnClickListener(v -> {
            Toast.makeText(this, "查看商品详情", Toast.LENGTH_SHORT).show();
            // In a real app, navigate to product detail page
        });
        
        // Buy again button
        btnBuyAgain.setOnClickListener(v -> {
            Toast.makeText(this, "再次购买: " + order.getProductTitle(), Toast.LENGTH_SHORT).show();
            // In a real app, add to cart or navigate to checkout
        });
    }
} 